package handler

import (
	"bike-vb/cache"
	"bike-vb/model"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

// 添加
func CarAdd(c *gin.Context) {
	car_no := c.PostForm("car_no")
	car_name := c.PostForm("car_name")
	car_code := c.PostForm("car_code")
	if car_no == "" || car_name == "" || car_code == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := model.Car{
		CarNo:   car_no,
		CarName: car_name,
		CarCode: car_code,
	}
	err := model.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

// 订单添加
func CarOrderAdd(c *gin.Context) {
	order_no := c.PostForm("order_no")
	order_from := c.PostForm("order_from")
	order_to := c.PostForm("order_to")
	order_good := c.PostForm("order_good")
	c_id, _ := strconv.Atoi(c.PostForm("c_id"))
	if order_no == "" || order_from == "" || order_to == "" || order_good == "" || c_id == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := model.CarOrder{
		OrderNo:   order_no,
		OrderFrom: order_from,
		OrderTo:   order_to,
		OrderGood: order_good,
		Cid:       c_id,
	}
	err := model.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

// 订单列表
func CarOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size

	/*
		SELECT
		  car_orders.id AS order_id,
		  cars.car_no AS car_no,
		  cars.car_name AS car_name,
		  cars.car_code as car_code,
		  car_orders.order_no AS order_no,
		  car_orders.order_from AS order_from,
		  car_orders.order_to AS order_to,
		  car_orders.order_good AS order_good
		FROM
		  car_orders
		  LEFT JOIN cars ON cars.id = car_orders.cid
	*/
	var list []model.CarOrderList
	redis_key := "car_order_list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("car_orders").
			Select(
				"car_orders.id AS order_id",
				"cars.car_no AS car_no",
				"cars.car_name AS car_name",
				"cars.car_code as car_code",
				"car_orders.order_no AS order_no",
				"car_orders.order_from AS order_from",
				"car_orders.order_to AS order_to",
				"car_orders.order_good AS order_good",
			).
			Joins("LEFT JOIN cars ON cars.id = car_orders.cid").
			Offset(offset).
			Limit(size).
			Debug().
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}

	var total int64
	model.DB.Model(&model.CarOrder{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "获取成功",
		"data":  list,
		"total": total,
	})
}

// 订单详情
func CarOrderDetail(c *gin.Context) {
	id := c.Query("id")
	/*
		SELECT
		  car_orders.id AS order_id,
		  cars.car_no AS car_no,
		  cars.car_name AS car_name,
		  cars.car_code as car_code,
		  car_orders.order_no AS order_no,
		  car_orders.order_from AS order_from,
		  car_orders.order_to AS order_to,
		  car_orders.order_good AS order_good
		FROM
		  car_orders
		  LEFT JOIN cars ON cars.id = car_orders.cid
		  WHERE car_orders.id=1
	*/
	var carOrder model.CarOrder
	redis_key := "car_order_detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("car_orders").
			Select(
				"car_orders.id AS order_id",
				"cars.car_no AS car_no",
				"cars.car_name AS car_name",
				"cars.car_code as car_code",
				"car_orders.order_no AS order_no",
				"car_orders.order_from AS order_from",
				"car_orders.order_to AS order_to",
				"car_orders.order_good AS order_good",
			).
			Joins("LEFT JOIN cars ON cars.id = car_orders.cid").
			Where("car_orders.id = ?", id).
			Debug().
			Find(&carOrder)
		jsonData, _ := json.Marshal(carOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &carOrder)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取成功",
		"data": carOrder,
	})
}
